import os
import zipfile
import random
from tqdm import tqdm

SrcDir = r"J:\Temp\Temp1"
DstDir = r"J:\Temp\zip\Circle\1205_RB_append"
ZipFileNum = 13

names = []

# for file in os.scandir(SrcDir):
#     if file.name[-1] == 'g':
#         names.append(file.name)

for root, dirs, files in os.walk(SrcDir):
    for file in files:
        if file[-1] == 'g':
            names.append(root + "\\" + file)

# 打乱顺序
# random.shuffle(names)

FilesInZip = int(len(names) / ZipFileNum)
FileRemain = len(names) % ZipFileNum
for i in range(ZipFileNum):
    z = zipfile.ZipFile(f"{DstDir}/{i + 1}.zip", 'w', zipfile.ZIP_DEFLATED)
    # if i != (ZipFileNum - 1):I:\ZipOutput\DeviceCircle2
    #     PartOfNames = names[i * FilesInZip:(i + 1) * FilesInZip]
    # else:
    #     PartOfNames = names[i * FilesInZip:]
    if i < FileRemain:
        PartOfNames = names[i * (FilesInZip + 1):(i + 1) * (FilesInZip + 1)]
    else:
        PartOfNames = names[FileRemain + FilesInZip * i:FileRemain + FilesInZip * (i + 1)]
    print("--------------------------------------%d.zip-------------------------------- " % (i + 1))
    print(PartOfNames)
    for name in tqdm(PartOfNames):
        z.write(name, name.split("\\")[-1])
        # z.write(SrcDir + "\\" + name, name)
        # z.write(SrcDir + "\\" + name[:-3] + "jpg", name[:-3] + "jpg")
        # print(name)
    z.close()
